#include "entity.h"
#include "images_diffuse.h"
#include "bullet.h"
#include "hitbox.h"
#include "tag.h"
#include "explosions.h"
#include "enemy.h"
#include "hitpoint.h"

#define PERIOD 80

declare_body(missile_ship, {
	size_t period;
})

declare_tick(missile_ship, {
	if(self->period == 0) {
		make_enemy_missile_bullet(parent->position, vxy(0.f, -5.f), NULL, 8);
		self->period = PERIOD;
	}
	else --self->period;
})

declare_draw(missile_ship, { })

declare_head(missile_ship)

entity *make_missile(vec location, int health) {
	entity *ent = ent_new();
	ent->position = vxy(location.x, 250);
	ent->tag = TAG_ENEMY;
	
	missile_ship *b = c_new(missile_ship);
	b->period = 30; /* Delay basically until we on onscreen but fire quickly */
	
	add_hitpoint_t(ent, vxy(-8, 0), 13, health);
	add_hitpoint_t(ent, vxy(15, 0), 12, health);
	add_hitpoint_t(ent, vxy(-5, 0), 11, health);
	add_hitpoint_t(ent, vxy(12, 0), 10, health);
	add_hitpoint_t(ent, vxy(-2, 0), 9, health);
	add_hitpoint_t(ent, vxy( 9, 0), 8, health);
	add_hitpoint_t(ent, vxy( 1, 0), 7, health);
	add_hitpoint_t(ent, vxy( 6, 0), 6, health);
	add_hitpoint_t(ent, vxy( 2, 0), 5, health);
	add_hitpoint_t(ent, vxy(12, -14), 4, health);
	add_hitpoint_t(ent, vxy(12, -11), 3, health);
	add_hitpoint_t(ent, vxy(12, -8), 2, health);
	add_hitpoint_t(ent, vxy(12, -5), 1, health);
	
	add_component(ent, get_animator_c(&sprite_ship_missile, 1, vxy(11, 17)));
	add_component(ent, b);
	add_component(ent, target_loc(location));
	
	add_hitbox(ent, -9, -2, 24, 4);
	
	return ent;
}